﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace SpringStudio.DAL
{
    /// <summary>
    /// 数据库操作异常处理
    /// </summary>
    internal static class SqlExceptionHandler
    {
        public static void Handle(Exception ex, string commondText, IDbDataParameter[] commandParameters)
        {
            string sql = commondText;
            if (commandParameters != null)
            {
                foreach (IDataParameter parameter in commandParameters)
                {
                    switch (parameter.GetType().ToString())
                    {
                        case "System.Int32":
                            sql = sql.Replace(parameter.ParameterName, string.Format("{0}", parameter.Value));
                            break;

                        case "System.String":
                            sql = sql.Replace(parameter.ParameterName, string.Format("'{0}'", parameter.Value));
                            break;

                        default:
                            sql = sql.Replace(parameter.ParameterName, string.Format("'{0}'", parameter.Value));
                            break;
                    }
                }
            }

            throw new Exception(string.Format("可能出错的SQL语句:{0}{1}错误信息:{2}", sql, Environment.NewLine, ex.Message), ex);
        }
    }
}
